<?php
namespace Agent\Controller;
use Think\Controller;
class CheckbillController extends BaseController {
    public function _initialize()
    {
        parent::_initialize();
        $g=session('agent');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
    }
	public function order(){
		$param=I('param.','','trim');
		$id=$param['id'];
		$order_no=trim($param['order_no']);
		$logistics_no=trim($param['logistics_no']);
		
		$m=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));
		$pu=M('Pushorder', C('DB_PREFIX_T'), C('db_config'));
		$m_info=$m->where(array('id'=>$id))->find();
		$order_ids=$m_info['ids'];
		$map['id']=array('in',"$order_ids");
		if(!empty($order_no)){
			$map['old_order_no']=array('like',"%$order_no%");
		}
		if(!empty($logistics_no)){
			$map['logistics_no']=array('like',"%$logistics_no%");
		}

		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;
		
		$count=$pu->where($map)->count();
		$list=$pu->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		// echo $pu->getlastsql();
		$this->assign('list',$list);
		$this->assign('count',$count);
		$this->display();
	}
    public function index(){
		// dump(session());
        $m=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));
		$param=I('param.','','trim');	
		$pay_no=$param['pay_no'];		
		$p=I('p')?I('p'):1;
		$listrows=20;
		$firstrow=($p-1)*$listrows;

		if(!empty($pay_no)){
			$map['pay_no'] = array('like','%'.$pay_no.'%');
        }
		//时间查询-begin
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		if(!empty($start_time) && empty($end_time)) {
			$start_time=strtotime($start_time);
			$map['_string']="add_time > '$start_time' ";
        }
        if(empty($start_time) && !empty($end_time)) {
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time < '$end_time' ";
        }
		if(!empty($start_time) && !empty($end_time)) {
			$start_time=strtotime($start_time);
			$end_time=strtotime($end_time)+3600*24;
			$map['_string']="add_time > '$start_time' and add_time < '$end_time'";
        }
		$map['agent_id']=session('agent.id');
		$map['pay_status']=1;
		// dump($param);
		//时间查询-end
		$count=$m->where($map)->count();
		$list=$m->where($map)->limit($firstrow.','.$listrows)->order('id desc')->select();
		$Page       = new \Think\Page($count,$listrows);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		foreach($list as $k=>$v){
			$list[$k]['add_time']=date('Y-m-d H:i:s',$v['add_time']);
		}
		$excel=I('excel');
		if($excel){
			$xlsName  = "对账单";
			$xlsCell  = array(
				array('fee','支付金额'),
				array('pay_no','支付单号'),
				array('add_time','下单时间')
			);
			$order=A('Order');
			$order->exportExcel($xlsName,$xlsCell,$list);
			die;
		}
		$this->assign("count",$count);
		$this->assign("list",$list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
    }
	public function add(){
		$role=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$role_list=$role->where(array('status'=>1))->select();
		$this->assign('role_list',$role_list);
		$this->display();
	}
	//添加
	public function confirm_add(){
			$m=M('Checkbill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$r=M('Role', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			$role_id=$param['role_list'];
			$cate=$param['cate'];
			if($cate!=1){
				$this->verification($param);
			}
			$map['mail_no']=$param['mail_no'];
			$map['order_no']=$param['order_no'];
			$map['_logic'] = 'OR';
			$dup=$m->where($map)->find();
			if($dup){
				$return['status']=0;
				$return['info']="订单号或运单号已经存在";
				$this->ajaxReturn($return);
			}
			$param['add_time']=time();
			// dump($param);
			unset($param['role_list']);
			$res=$m->add($param);
			if($res){
				if($cate==1){//更新充值金额
					$role_data['fee']=array('exp','fee+'.$param['fee']);
					$role_data['remain_fee']=array('exp','remain_fee+'.$param['fee']);
					$r->where(array('id'=>$role_id))->save($role_data);
				}
				$return['status']=1;
				$return['info']="添加成功";
				$this->ajaxReturn($return);
			}
	}
	//自动添加对账单
	//fee费用
	public function confirm_adds($fee,$order_no,$cate=2){
		$g=session('agent');
		$gc=session('agent.childs');
		
		$m=M('Checkbill', C('DB_PREFIX_T'), C('db_config'));
		$u=M('Role', C('DB_PREFIX_T'), C('db_config'));
		$data['fee']=$fee;
		$data['order_no']=$order_no;
		$data['cate']=$cate;
		$data['add_time']=time();
		
		if(!empty($gc)){//从用户
			$data['role_id']=$gc['id'];
			$remain_fee=$u->where(array('id'=>$gc['id']))->getField('fee');
			$data['remain_fee']=$remain_fee;
		}
		$res=$m->add($data);
		// dump($data);
		if($res){
			// $return['status']=1;
			// $return['message']="添加成功";
			// $this->ajaxReturn($return);
			return 1;
		}else{
			return 0;
		}
	}
	public function verification($param){
			// dump($param);
			if(empty($param['fee'])){
				$return['status']=0;
				$return['info']="费用不能为空";
				$this->ajaxReturn($return);
			}
			if(empty($param['mail_no'])){
				$return['status']=0;
				$return['info']="运单号不能为空";
				$this->ajaxReturn($return);
			}
			if(empty($param['order_no'])){
				$return['status']=0;
				$return['info']="订单号不能为空";
				$this->ajaxReturn($return);
			}
			if(empty($param['cate'])){
				$return['status']=0;
				$return['info']="费用类型不能为空";
				$this->ajaxReturn($return);
			}
	}
	public function edit(){
		$m=M('Checkbill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$id=I('id');
		$info=$m->find($id);
		$this->assign('detail',$info);
		$this->display();
	}
	//编辑
	public function confirm_edit(){
			$m=M('Checkbill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
			$param = I('param.','','trim');
			// dump($param);
			$this->verification($param);
			$maps['id']=$param['id'];
			$map['mail_no']=$param['mail_no'];
			$map['order_no']=$param['order_no'];
			$map['_logic'] = 'OR';
			$where['id']=array('neq',$param['id']);
			$where['_complex'] = $map;
			$dup=$m->where($where)->find();
			// echo $m->getlastsql();
			if($dup){
				$return['status']=0;
				$return['info']="订单号或运单号已经存在";
				$this->ajaxReturn($return);
			}
			$res=$m->where($maps)->save($param);
			if($res!==false){
				$return['status']=1;
				$return['info']="编辑成功";
				$this->ajaxReturn($return);
			}
	}
	//删除
	public function del(){
		$id=trim(I('id'));
		$m=M('Checkbill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$map['id']=$id;
		$status=$m->where($map)->delete();

		if($status!==false){
            echo "y";
        }else{
            echo "操作失败";
        }
	}
	//批量删除
	public function delAll(){
		$ids=I('ids');
		$map['id']=array('in',$ids);
		$m=M('Checkbill', C('DB_PREFIX_T'), C('DB_TYPE') . '://' . C('DB_USER') . ':' . C('DB_PWD') . '@' . C('DB_HOST') . ':' . C('DB_PORT') . '/' . C('erp_dbname'));
		$status=$m->where($map)->delete();
		if($status){
			$return['status']=1;
			$return['info']="删除成功";
			$this->ajaxReturn($return);
		}else{
			$return['status']=0;
			$return['info']="出错";
			$this->ajaxReturn($return);
		}
	}

}